package com.chixing.controller;

import com.chixing.service.CouponService;
import com.chixing.util.ServerResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CouponController {

    @Autowired
    private CouponService couponService;

    @GetMapping("coupon/{memberId}")
    public ModelAndView getAll(@PathVariable("memberId") Integer memberId) {

        ServerResult serverResult = couponService.getAll(memberId);
        ModelAndView mav = new ModelAndView();

        if (serverResult.getCode() == 200) {
            mav.addObject("coupon", serverResult.getData());
            mav.setViewName("coupon/coupon");
        } else {
            mav.addObject("no_coupon", serverResult.getData());
            mav.setViewName("coupon/coupon");
        }

        return mav;
    }

    /**
     * @param couponId
     * @param memberId
     * @return
     */
    @GetMapping("coupon/receive")
    public ModelAndView receiveCoupon(@Param("couponId") Integer couponId, @Param("memberId") Integer memberId) {
        System.out.println("couponId:" + couponId);
        System.out.println("memberId:" + memberId);
        ModelAndView mav = new ModelAndView();

        ServerResult serverResult = couponService.receiveCoupon(couponId, memberId);
        if (serverResult.getCode() == 200)
            mav.addObject("receive_coupon", serverResult.getData());
        else
            mav.addObject("receive_coupon", serverResult.getData());
        mav.setViewName("redirect:/coupon/" + memberId);

        return mav;
    }

    @GetMapping("customer/coupon/{memberId}")
    public ModelAndView getUserCoupon(@PathVariable("memberId") Integer memberId) {

        ServerResult serverResult = couponService.getUserCoupon(memberId);
        ModelAndView mav = new ModelAndView();

        if (serverResult.getCode() == 200) {
            mav.addObject("coupon", serverResult.getData());

        } else {
            mav.addObject("toreceivecoupon", serverResult.getData());

        }
        mav.setViewName("coupon/mycoupon");
        return mav;
    }


}
